<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 检查验证码
 *
 * @author      Eden
 * @datetime    2016/11/30 21:33
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Check_Model extends API_Model
{
    /**
     * 配置
     * @var array
     */
    private $useAge = '';
    private $key = '';

    /**
     * @var array 发送时间间隔
     */
    private $conf = array(
        'regist' => array(
            'key' => 'Regist',
            'lifeTime' => 60,       // 离下次发送的时间间隔
            'saveTime' => 600,      // 在缓存中保存的时间
        ),
        'findpw' => array(
            'key' => 'Findpw',
            'lifeTime' => 60,
            'saveTime' => 600,
        )
    );

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
        $this->load->driver('cache');
    }

    /**
     * 主程序
     * @param array $params
     */
    public function run($params = array())
    {
        // 验证参数
        $this->_checkParams($params);
        if ($this->checkSms($params)) {
            $send['state'] = true;
            $send['data'] = array(
                'msg' => '验证成功'
            );
        } else {
            $send['state'] = false;
            $send['error'] = '验证失败';
        }
        $this->output->send($send);
    }

    /**
     * 验证 Code
     * @param $params
     * @return bool
     */
    public function checkSms($params)
    {
        $key = 'API:Member:' . $this->key['key'] . ':' . $params['phone'];
        $cacheData = $this->cache->redis->get($key);
        if ($cacheData) {
            $code = value($cacheData, 'code');
            if ($code == $params['code']) {
                $this->cache->redis->delete($key);
                return true;
            }
        } else {
            $this->output->send(array(
                'state' => false,
                'error' => '验证码已失效'
            ));
        }
        return false;
    }

    /**
     * 验证参数
     * @param $params
     * @return bool
     */
    private function _checkParams($params)
    {
        $send['state'] = false;
        do {
            if (empty($params['phone']) || !preg_match('/^1[34578]\d{9}$/', $params['phone'])) {
                $send['error'] = '手机号格式错误';
                break;
            }
            if (empty($params['useAge'])) {
                $send['error'] = '无效参数类型';
                break;
            }
            if (empty($params['code']) || !preg_match('/^\d{4}$/', $params['code'])) {
                $send['error'] = '参数错误';
                break;
            }

            $useAge = strtolower($params['useAge']);
            $this->useAge = $useAge;        // regist,resetpassword

            if (empty($this->conf[$useAge])) {
                $send['error'] = '不支付的校验类型';
                break;
            }

            $this->key = $this->conf[$useAge];

            if (empty($this->key)) {
                $send['error'] = '配置无效';
                break;
            }
            return true;
        } while(false);
        $this->output->send($send);
    }
}
